package metier;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import model.HoraireAvion;
import model.HoraireTrain;
import model.VoyageAvion;
import model.VoyageTrain;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupOperations {
	public static VoyageTrain get(VoyageTrain voyage) {
		Document doc;
		Elements tds;
		List<HoraireTrain> horaires = new ArrayList<HoraireTrain>();
		try {
			doc = Jsoup
					.connect(
							"http://www.oncf.ma/Pages/ResultatsHoraire.aspx?depart="
									+ voyage.getGareDepart().getNom()
									+ "&arrivee="
									+ voyage.getGareArrivee().getNom()
									+ "&CodeRD="
									+ voyage.getGareDepart().getCodeR()
									+ "&CodeGD="
									+ voyage.getGareDepart().getCodeG()
									+ "&CodeRA="
									+ voyage.getGareArrivee().getCodeR()
									+ "&CodeGA="
									+ voyage.getGareArrivee().getCodeG()
									+ "&heure=0000&date="
									+ voyage.getDateDepart())
					.timeout(30 * 1000).get();
			for (Element tr : doc.select("table tbody tr")) {
				tds = tr.select("td");
				if (tds.size() == 5)
					horaires.add(new HoraireTrain(tds.get(0).text(), tds.get(1)
							.text(), tds.get(2).text(), voyage));
			}
			if (horaires.size() != 0)
				horaires.remove(0);
			voyage.setHorairesTrain(horaires);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return voyage;
	}

	public static VoyageAvion get(VoyageAvion voyageavion) {
		Document doc;
		Elements tds;
		List<HoraireAvion> horaires = new ArrayList<HoraireAvion>();
		try {
			doc = Jsoup
					.connect(
							"http://www.onda.ma/ONDA/Fr/Espaces/EspacePassagers/LeGuideHoraire/GuideHoraire.htm?&ddAeroport1="
									+ voyageavion.getVilleDepart()
											.getCodeville()
									+ "&ddAeroport2=1559&Sens=D&DateVol="
									+ voyageavion.getDateVol()
									+ "&ddheure=00:00&Aeroport="
									+ voyageavion.getVilleDepart()
											.getNomVille()
									+ "&AeroportTous=TOUS%20LES%20A%C3%89ROPORTS&selectedIndexdd1=6&selectedIndexdd2=0&selectedIndexradio=0&heure=0&ddCompagnie=Aucun")
					.timeout(30 * 1000).get();
			for (Element tr : doc
					.select("#RechercheHoraire1_Panel2 #Layer1 table tbody tr")) {
				tds = tr.select("td");
				if (tds.size() == 7) {
					horaires.add(new HoraireAvion(cleanSpace(tds.get(0)
							.toString()), cleanSpace(tds.get(1).toString()),
							cleanSpace(tds.get(2).toString()), cleanSpace(tds
									.get(3).toString()), cleanSpace(tds.get(4)
									.toString()), cleanSpace(tds.get(5)
									.toString()), cleanSpace(tds.get(6)
									.toString()), voyageavion));
				}
			}
			voyageavion.setHorairesavion(horaires);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return voyageavion;
	}

	public static String cleanSpace(String city) {
		String result = city.substring(10, city.length());
		result = result.substring(0, result.length() - 5);
		return result;
	}
}
